#include "../mqserver/Consumer.hpp"
#include <gtest/gtest.h>
#include <iostream>
#include <unordered_map>
using namespace std;
mq::ConsumerManager::ConsumerManagerPtr cmp;
class ConsumerTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        cmp = make_shared<mq::ConsumerManager>();
        cmp->initQueueConsumer("queue1");
        cmp->initQueueConsumer("queue2");
        cmp->initQueueConsumer("queue3");
        cout << "测试环境初始化" << endl;
    }
    virtual void TearDown() override
    {
        cout << "测试环境清理" << endl;
    }
};
TEST(consumer, insert_test)
{
    cmp->create("consumer1", "queue1", true, nullptr);
    cmp->create("consumer2", "queue1", true, nullptr);
    cmp->create("consumer3", "queue1", true, nullptr);
    cmp->create("consumer4", "queue2", true, nullptr);
    cmp->create("consumer5", "queue2", true, nullptr);
    cmp->create("consumer6", "queue2", true, nullptr);
    cmp->create("consumer7", "queue3", true, nullptr);
    cmp->create("consumer8", "queue3", true, nullptr);
    cmp->create("consumer9", "queue3", true, nullptr);
}

TEST(consumer, select_test)
{
    ASSERT_EQ(cmp->empty("queue1"), false);
    ASSERT_EQ(cmp->empty("queue2"), false);
    ASSERT_EQ(cmp->empty("queue3"), false);
    ASSERT_EQ(cmp->exists("consumer1", "queue1"), true);
    ASSERT_EQ(cmp->exists("consumer2", "queue1"), true);
    ASSERT_EQ(cmp->exists("consumer3", "queue1"), true);
    ASSERT_EQ(cmp->exists("consumer4", "queue2"), true);
    ASSERT_EQ(cmp->exists("consumer5", "queue2"), true);
    ASSERT_EQ(cmp->exists("consumer6", "queue2"), true);
    ASSERT_EQ(cmp->exists("consumer7", "queue3"), true);
    ASSERT_EQ(cmp->exists("consumer8", "queue3"), true);
    ASSERT_EQ(cmp->exists("consumer9", "queue3"), true);
    ASSERT_EQ(cmp->choose("queue1")->_tag, "consumer1");
    ASSERT_EQ(cmp->choose("queue1")->_tag, "consumer2");
    ASSERT_EQ(cmp->choose("queue1")->_tag, "consumer3");
    ASSERT_EQ(cmp->choose("queue2")->_tag, "consumer4");
    ASSERT_EQ(cmp->choose("queue2")->_tag, "consumer5");
    ASSERT_EQ(cmp->choose("queue2")->_tag, "consumer6");
    ASSERT_EQ(cmp->choose("queue3")->_tag, "consumer7");
    ASSERT_EQ(cmp->choose("queue3")->_tag, "consumer8");
    ASSERT_EQ(cmp->choose("queue3")->_tag, "consumer9");
}
TEST(consumer, delete_test)
{
    cmp->remove("consumer2", "queue1");
    ASSERT_EQ(cmp->choose("queue1")->_tag, "consumer1");
    ASSERT_EQ(cmp->choose("queue1")->_tag, "consumer3");
    cmp->remove("consumer5", "queue2");
    ASSERT_EQ(cmp->choose("queue2")->_tag, "consumer4");
    ASSERT_EQ(cmp->choose("queue2")->_tag, "consumer6");
    cmp->remove("consumer8", "queue3");
    ASSERT_EQ(cmp->choose("queue3")->_tag, "consumer7");
    ASSERT_EQ(cmp->choose("queue3")->_tag, "consumer9");
    ASSERT_EQ(cmp->exists("consumer1", "queue1"), true);
    ASSERT_EQ(cmp->exists("consumer2", "queue1"), false);
    ASSERT_EQ(cmp->exists("consumer3", "queue1"), true);
    ASSERT_EQ(cmp->exists("consumer4", "queue2"), true);
    ASSERT_EQ(cmp->exists("consumer5", "queue2"), false);
    ASSERT_EQ(cmp->exists("consumer6", "queue2"), true);
    ASSERT_EQ(cmp->exists("consumer7", "queue3"), true);
    ASSERT_EQ(cmp->exists("consumer8", "queue3"), false);
    ASSERT_EQ(cmp->exists("consumer9", "queue3"), true);

    cmp->destroyQueueConsumer("queue1");
    ASSERT_EQ(cmp->exists("consumer1", "queue1"), false);
    ASSERT_EQ(cmp->exists("consumer2", "queue1"), false);
    ASSERT_EQ(cmp->exists("consumer3", "queue1"), false);
    cmp->clear();
    ASSERT_EQ(cmp->exists("consumer4", "queue2"), false);
    ASSERT_EQ(cmp->exists("consumer5", "queue2"), false);
    ASSERT_EQ(cmp->exists("consumer6", "queue2"), false);
    ASSERT_EQ(cmp->exists("consumer7", "queue3"), false);
    ASSERT_EQ(cmp->exists("consumer8", "queue3"), false);
    ASSERT_EQ(cmp->exists("consumer9", "queue3"), false);
}
int main(int argc, char *argv[])
{
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}